﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Master.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.App.Master.Bean;
using Shpm.System;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.App.Commons.Utils;


public partial class App_Input_EvaluationSrhForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            initPageControls();
            if (Context.Items.Contains("NAME"))
            {
                this.ViewState["NAME"] = Context.Items["NAME"];
                txtName.Text = Convert.ToString(this.ViewState["NAME"]);
            }
            if (Context.Items.Contains("TYPE"))
            {
                this.ViewState["TYPE"] = Context.Items["TYPE"];
                drpType.SelectedValue = Convert.ToString(this.ViewState["TYPE"]);
            }
            if (Context.Items.Contains("DO"))
            {
                this.ViewState["DO"] = Context.Items["DO"];
            }
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.ViewState["NAME"] = txtName.Text;
        this.ViewState["TYPE"] = drpType.SelectedValue;
        this.ViewState["DO"] = "srh";
    }

    /*
     * 函数名：initPageControls
     * 功能：控件数据绑定
     * 参数：无
     * 说明：将数据字典中的数据绑定在指定的控件上
     */
    private void initPageControls()
    {
        DishDao dao = new DishDao();
        CommonDictionaryBean bean = new CommonDictionaryBean();

        // 课程分类
        bean.Catalog = "CURRICULUM_TYPE";
        IList lstTime = dao.FindByCatalog(bean);
        lstTime.Insert(0, new CommonDictionaryBean());
        drpType.DataSource = lstTime;
        drpType.DataBind();
}

    /*
     * 函数名：initDic
     * 功能：控件数据绑定
     * 参数：无
     * 说明：将数据字典中的数据绑定在指定的控件上
     */
    private void initDic()
    {

        CommonDictionaryBean paramBean = new CommonDictionaryBean();
        CommonDictionaryDao dao = new CommonDictionaryDao();

        paramBean.Catalog = "CURRICULUM_TYPE";
        initAll(dao, paramBean, this.drpType);
    }

    /*
     * 函数名：initAll
     * 功能  ：控件数据绑定
     * 参数  ：dao(数据字典的DAO）
     *       ：paramBean(数据字典的Bean）
     *       ：obj（控件）
     * 说明  ：
     */
    private void initAll(CommonDictionaryDao dao, CommonDictionaryBean paramBean, ListControl obj)
    {
        IList dataList = dao.FindByCatalog(paramBean);

        obj.DataSource = dataList;
        obj.DataBind();
        if (SetIndex(dataList) != -1)
            obj.SelectedIndex = SetIndex(dataList);
    }

    /*
    * 函数名：SetIndex
    * 功能：设置默认值
    * 参数：list（数据字典的集合）
    * 说明：将数据字典中的数据绑定在指定的控件上
    */
    private int SetIndex(IList list)
    {
        CommonDictionaryBean dataBean = new CommonDictionaryBean();
        for (int i = 0; i < list.Count; i++)
        {
            dataBean = (CommonDictionaryBean)list[i];
            if (dataBean.Default == true) return i;
        }
        return -1;
    }
    protected override void OnPreRender(EventArgs e)
    {
        if(Convert.ToString(this.ViewState["DO"]) == "srh")
        {
            EvaluationSrhBean dataBean = new EvaluationSrhBean();
            EvaluationSrhDao Dao = new EvaluationSrhDao();
            dataBean.CurriculumName = Convert.ToString(this.ViewState["NAME"]);
            dataBean.CurriculumType = Convert.ToString(this.ViewState["TYPE"]);
            IList list= Dao.ListClass(dataBean);
            GridView1.DataSource = list;
            GridView1.DataBind();
        }
    }

    protected void GridView1_OnRowEditing(object sender, GridViewEditEventArgs e)
    {
        string curriculumId = Convert.ToString((sender as GridView).DataKeys[e.NewEditIndex].Values[0]);
        Context.Items["CURRICULUM_ID"] = curriculumId;
        Context.Items["NAME"] = this.ViewState["NAME"];
        Context.Items["TYPE"] = this.ViewState["TYPE"];
        Context.Items["DO"] = this.ViewState["DO"];
        string url = "~/App/Input/EvaluationUpdForm.aspx";
        ControllerUtils.Transfer(Context, url, true);   
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string curriculumId = Convert.ToString((sender as GridView).DataKeys[e.RowIndex].Values[0]);
        Context.Items["CURRICULUM_ID"] = curriculumId;
        Context.Items["TEACHER_NAME"] = this.GridView1.Rows[e.RowIndex].Cells[2].Text;
        Context.Items["CLASS_NAME"] = this.GridView1.Rows[e.RowIndex].Cells[1].Text;
        Context.Items["NAME"] = this.ViewState["NAME"];
        Context.Items["TYPE"] = this.ViewState["TYPE"];
        Context.Items["DO"] = this.ViewState["DO"];
        string url = "~/App/Input/EvaluationTeacherUpdForm.aspx";
        ControllerUtils.Transfer(Context, url, true);  
    }
}
